package com.tanhua.app.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.api.BlackListApi;
import com.tanhua.api.QuestionApi;
import com.tanhua.api.SettingsApi;
import com.tanhua.app.intercept.UserHolder;
import com.tanhua.model.pojo.Question;
import com.tanhua.model.pojo.Settings;
import com.tanhua.model.pojo.User;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    /**
     * Servicec层执行查询通用设置的逻辑
     * @return
     */
    public SettingsVo settings() {
        //1.创建Vo对象
        SettingsVo settingsVo = new SettingsVo();

        //2.从线程中获取该登录用户的id和手机号,并添加到settings对象中
        Long userId = UserHolder.getUserId();
        String mobile = UserHolder.mobile();
        settingsVo.setId(userId);
        settingsVo.setPhone(mobile);

        //3.获取用户通用设置的陌生人问题，将问题内容添加到settingsVo对象中(查询Question表，则需要创建该接口和实现类查询)
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢java不？": question.getTxt();
        settingsVo.setStrangerQuestion(txt);

        //4.获取用户通用设置中的通用设置信息（查询Settings表，则需要创建该接口和实现类查询）
        Settings settings = settingsApi.findByUserId(userId);
        //判断该用户是否存在通用设置
        if (settings != null){
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
        }

        //5.返回对象结果
        return settingsVo;

    }

    /**
     * 通用设置中陌生人问题处理逻辑
     * @param content
     */
    public void saveQuestion(String content) {
        //1.获取当前用户的id
        Long userId = UserHolder.getUserId();

        //2.调用接口根据id查询用户的陌生人问题
        Question question = questionApi.findByUserId(userId);

        //3.判断该问题是否存在
        if (question ==null){
            //不存在，新增问题内容
            //创建对象封装内容
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            //通过接口新增增内容
            questionApi.saveQuestion(question);
        }else{
            //存在，则更新内容
            question.setTxt(content);
            //通过接口修改内容
            questionApi.updateQuestion(question);
        }
    }

    /**
     * 设置用户通用设置中的通用设置信息
     * @param param
     */
    public void saveSettings(Map param) {
        //1.获取参数
        boolean likeNotification = Boolean.parseBoolean(param.get("likeNotification").toString());
        boolean pinglunNotification = Boolean.parseBoolean(param.get("pinglunNotification").toString());
        boolean gonggaoNotification = Boolean.parseBoolean(param.get("gonggaoNotification").toString());

        //2.获取当前登录用户的id
        Long userId = UserHolder.getUserId();

        //3.调用接口根据id查询用户的通用设置
        Settings settings = settingsApi.findByUserId(userId);

        //4.判断是否存在
        if (settings == null){
            //该用户不存在通用设置，新增通用设置
            //创建对象
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            //通过接口新增增内容
            settingsApi.saveSettings(settings);
        }else{
            //存在，则修改该用户的通用设置
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            //通过接口修改内容
            settingsApi.updateSettings(settings);
        }

    }


    /**
     * 黑名单非分页查询
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult findBlackList(Integer page, Integer pageSize) {
        //1.调用api接口，根据用户id 进行分页查询  插叙黑名但用户的详细信息
        //获取当前用户的id
        Long userId = UserHolder.getUserId();
        Page pages = blackListApi.findByUserId(userId,page,pageSize);

        //2.封装放回对象，返回数据
        return new PageResult(page,pageSize, (int) pages.getTotal(),pages.getRecords());
    }

    /**
     * 根据移除黑名单
     * @param blackUserId
     */
    public void deleteBlack(Long blackUserId) {
        //1.获取当前登录用户id
        Long userId = UserHolder.getUserId();
        //2.调用api接口，删除登录用户中黑名单id为blackUserId
        blackListApi.deleteBlack(userId,blackUserId);
    }
}
